package com.example.demo.security;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class JwtAuthenticationToken extends UsernamePasswordAuthenticationToken {
    private final Long userId;  // 新增字段

    // 构造方法1：用于认证前（未授权）
    public JwtAuthenticationToken(Object principal, Object credentials) {
        super(principal, credentials);
        this.userId = null;
    }

    // 构造方法2：用于认证后（已授权）
    public JwtAuthenticationToken(Object principal, Object credentials,
                                  Collection<? extends GrantedAuthority> authorities,
                                  Long userId) {
        super(principal, credentials, authorities);
        this.userId = userId;
    }

    // 获取用户ID的方法
    public Long getUserId() {
        return this.userId;
    }
}